package cn.ehanghai.htz.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static String DATE_FORMAT = "yyyy-MM-dd";
	public static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 获取当前日期
	 * @return
	 */
	public static String getCurrentDate() {
		String datestr = null;
		SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT);
		datestr = df.format(new Date());
		return datestr;
	}

	public static String getCurrentDateLong(){
		String datestr = null;
		SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT);
		datestr = df.format(new Date());
		return datestr;
	}
	/**
	 * 将日期格式日期转换为字符串格式
	 *
	 *
	 * @param date
	 * @return
	 *
	 */
	public static String dateToString(Date date) {
		String datestr = null;
		SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT);
		datestr = df.format(date);
		return datestr;
	}

	/**
	 * 日期格式转时间字符串
	 * @param date
	 * @return
	 */
	public static String dateToStringShort(Date date){
		String datestr = null;
		SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT);
		datestr = df.format(date);
		return datestr;
	}

	/**
	 * 获取输入日期的当月第一天
	 *
	 *
	 * @param date
	 *      输入日期
	 * @return
	 *
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		cd.set(Calendar.DAY_OF_MONTH, 1);
		return cd.getTime();
	}


	public static Date getDateByString(String str,String fomate) {
		Date date = new Date();
		try {
			date=new SimpleDateFormat(fomate).parse(str);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return date;
		
	}

	public static String getString(Object object) {
		// TODO Auto-generated method stub
		String str="";
		if(object!=null){
			str=object.toString();
		}
		return str;
	}

	/**
	 * 是否最后一天
	 * @param date
	 * @return
	 */
	public static  boolean isLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH) == calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 是否第一天
	 * @param date
	 * @return
	 */
	public static boolean isFirstDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		System.out.println(calendar.get(Calendar.MONTH));
		return calendar.get(Calendar.DAY_OF_MONTH) == 1;
	}

	public static void main(String[] args) {
//		boolean flag=isFirstDayOfMonth(new Date());
//		System.out.println(flag);
		System.out.println(getCurrentDateLong());
//		Calendar c=Calendar.getInstance();
//		c.add(c.MONTH,-1);
//		SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT);
//		String gtimelast=df.format(c.getTime());
//		System.out.println(gtimelast);
//		int lastMouthmaxday=c.getActualMaximum(Calendar.DAY_OF_MONTH);
//		System.out.println(lastMouthmaxday);
//		c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),lastMouthmaxday,23,59,59);
//		//按格式输出
//		String gtime=df.format(c.getTime());
//		System.out.println(gtime);
//		SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-01");
//		String gtime2=df2.format(c.getTime());
//		System.out.println(gtime2);

	}
}
